/*-
 * RequestBody.java
 *
 * Date: 2017年1月5日
 * Time: 上午10:36:12
 *
 * 版权所有©2012-2013 rengh
 */

package com.lmx.kathHack.serialize;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * #{description}
 * 
 * @author lmx
 * custom kath RequestBody
 */

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import io.katharsis.request.dto.DataBody;
import io.katharsis.request.dto.RequestBody;

@JsonDeserialize(using = RequestBodyDeserializerExt.class)
public class RequestBodyExt extends RequestBody{

    /**
     * Can be either of type {@link DataBodyExt} or {@link Iterable} of {@link DataBodyExt}.
     */
    private Object data;

    public Object getData() {
        return data;
    }

    public DataBodyExt getSingleData() {
        return (DataBodyExt) data;
    }

    public Iterable<DataBody> getMultipleData() {
        List<DataBody> list = new ArrayList<DataBody>();
        Iterator<DataBody> it = ((Iterable<DataBody>) data).iterator();
        while(it.hasNext()){
            DataBody item = it.next();
            DataBodyExt itemExt = new DataBodyExt();
            itemExt.setAttributes(item.getAttributes());
            itemExt.setId(item.getId());
            itemExt.setType(item.getType());
            itemExt.setRelationships(item.getRelationships());
            list.add(itemExt);
        }
        return list;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public boolean isMultiple() {
        return data != null &&
                Iterable.class.isAssignableFrom(data.getClass());
    }
}